import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

extension ImgPath on String {
  String get imgPath {
    return 'assets/images/${this}';
  }

  ///项目列表
  String get imgPathHome {
    return 'assets/images/home/${this}';
  }

  ///项目列表
  String get imgPathProList {
    return 'assets/images/pro_list/${this}';
  }

  ///每日报告
  String get imgPathReport {
    return 'assets/images/report/${this}';
  }

  ///我的
  String get imgPathMe {
    return 'assets/images/me/${this}';
  }

  ///统计分析
  String get imgPathStatistics {
    return 'assets/images/statistics/${this}';
  }

  ///招引工作
  String get imgPathAttract {
    return 'assets/images/attract/${this}';
  }

  ///领导批示
  String get imgPathInstruct {
    return 'assets/images/instruct/${this}';
  }

  ///登录注册
  String get imgPathLogin {
    return 'assets/images/login/${this}';
  }
}

ImageProvider loadImageProvider(
    {String? url, String? placeholder, double scale = 1.0}) {
  if (url == null || url.isEmpty) {
    return AssetImage(
      placeholder ?? 'default_pic.png'.imgPathMe,
    );
  }
  if (url.startsWith('http')) {
    return NetworkImage(url);
  } else {
    return FileImage(File(url));
  }
}

Widget loadPathImage(
    {String? url,
    String? placeholder,
    double? width,
    double? height,
    Color? color,
    int borderradius = 0,
    BoxFit fit = BoxFit.cover}) {
  if (url == null || url.isEmpty) {
    return Image.asset(
      placeholder ?? 'pic_quesheng.png'.imgPathMe,
      width: width,
      height: height,
      fit: BoxFit.fill,
    );
  }
  if (url.startsWith('http')) {
    return ClipRRect(
      borderRadius: BorderRadius.all(Radius.circular(borderradius.r)),
      child: Image.network(
        url,
        width: width,
        height: height,
        fit: fit,
        // progressIndicatorBuilder: (context, url, progress) {
        //   return CupertinoActivityIndicator(
        //     radius: 15.r,
        //   );
        // },
        // errorWidget: (context, str, obj) {
        //   return loadImage(url: placeholder);
        // },
      ),
    );
  } else {
    return Image.file(
      File(url),
      width: width,
      height: height,
      color: color,
      fit: fit,
    );
  }
}

Widget loadImage(
    {String? url,
    String? placeholder,
    double? width,
    double? height,
    Color? color,
    int borderradius = 0,
    BoxFit fit = BoxFit.cover}) {
  if (url == null || url.isEmpty) {
    return Image.asset(
      placeholder ?? 'pic_quesheng.png'.imgPathMe,
      width: width,
      height: height,
      fit: BoxFit.fill,
    );
  }
  if (url.startsWith('http')) {
    return ClipRRect(
      borderRadius: BorderRadius.all(Radius.circular(borderradius.r)),
      child: Image.network(
        url,
        width: width,
        height: height,
        fit: fit,
        // progressIndicatorBuilder: (context, url, progress) {
        //   return CupertinoActivityIndicator(
        //     radius: 15.r,
        //   );
        // },
        // errorWidget: (context, str, obj) {
        //   return loadImage(url: placeholder);
        // },
      ),
    );
  } else {
    return Image.asset(
      url,
      width: width,
      height: height,
      color: color,
      fit: fit,
    );
  }
}

Widget loadAvater(
    {String? url,
    String? placeholder,
    required double size,
    BoxFit fit = BoxFit.cover}) {
  return ClipOval(
    child: loadImage(
        url: url,
        placeholder: 'Avatar_default.png'.imgPathMe,
        width: size,
        height: size,
        fit: fit),
  );
}
